package com.psytech.commons.configuration.spring;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.configuration.ConfigurationException;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.psytech.commons.configuration.ConfigurationLoader;
import com.psytech.commons.configuration.ExtendedPropertiesConfiguration;

public class PlaceholderConfigurer extends PropertyPlaceholderConfigurer {

	private static Logger LOG = Logger.getLogger(PlaceholderConfigurer.class.getName());

	public PlaceholderConfigurer() throws ConfigurationException {
		super();
		final ExtendedPropertiesConfiguration configuration = ConfigurationLoader.load();
		this.setProperties(configuration.getProperties());
	}

	public PlaceholderConfigurer(final String fileName) throws ConfigurationException {
		final ExtendedPropertiesConfiguration configuration = new ExtendedPropertiesConfiguration(new File(fileName));
		this.setProperties(configuration.getProperties());
	}

	public PlaceholderConfigurer(final String fileName, final String configFilePropertyName) throws ConfigurationException {
		File propertyFile;
		if (configFilePropertyName != null && System.getProperty(configFilePropertyName) != null) {
			LOG.log(Level.WARNING, "Utilisation de la variable : " + configFilePropertyName);
			propertyFile = new File(System.getProperty(configFilePropertyName));
		} else {
			propertyFile = new File(fileName);
		}
		final ExtendedPropertiesConfiguration configuration = new ExtendedPropertiesConfiguration(propertyFile);
		this.setProperties(configuration.getProperties());
	}
}
